package dataStream;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @desc
 * @Author xuyouchang
 * @Date 2021/10/8
 */
public class FlatMapFuncTest {
    public static void main(String[] args) throws Exception {
        List<String> data = new ArrayList<>();
        data.add("xyc01");
        data.add("xyc02");
        data.add("xyc03");
        data.add("yc01");
        data.add("yc02");

        final StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> dataStream = env.fromCollection(data);
        DataStream<String> result = dataStream.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                Arrays.stream(value.split("0")).forEach(out::collect);
            }
        });

        dataStream.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return s.contains("xyc");
            }
        });

        dataStream.map(new RichMapFunction<String, Object>() {
            @Override
            public Object map(String s) throws Exception {
                return null;
            }

            //声明周期
            @Override
            public void open(Configuration parameters) throws Exception {
                System.out.println("open........");
            }

            @Override
            public void close() throws Exception {
                System.out.println("close........");
            }
        });


        result.print();
        env.execute();
    }
}
